package medium搜索旋转排序数组;

public class Solution {
    public int search(int[] nums, int target) {
        int n = nums.length;
        if (n == 0) {
            return -1;
        }
        if (n == 1) {
            return nums[0] == target ? 0 : -1;
        }
        int k = 0;
        int len = nums.length-1;
        while(k<=len){
            int mid = (int)(k+len)/2;
            if (nums[mid] == target)
                return mid;
            else if (nums[mid] < nums[len]) {
                if (nums[mid] < target && target <= nums[len])
                    k = mid + 1;
                else
                    len = mid - 1;
            }
            else{
                if(nums[k] <= target && target < nums[mid])
                    len = mid-1;
                else
                    k = mid+1;
            }
        }
        return -1;
    }
}
